﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using FlexCell;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;
using ModelValidation.Commands;
using ModelValidation.Utils;

namespace ModelValidation.FireCompartment
{
	public partial class DlgFireDefAreaResult : System.Windows.Forms.Form
	{
		[DllImport("user32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Auto, ExactSpelling = true, SetLastError = true)]
		internal static extern void MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

		public DlgFireDefAreaResult()
		{
			this.InitializeComponent();
		}

		public DlgFireDefAreaResult(ExternalCommandData cmdData, List<ViewPlan> lstView)
		{
			this.InitializeComponent();
			this.m_Revit = cmdData;
			this.m_lstViewPlan = lstView;
			this.RefreshListView();
			this.m_bExpand = false;
			this.ShowHideMoreParam();
			string fileName = Product.InstallLocation + "\\Data\\FireDefAreaRules.xml";
			this.gridRule.LoadFromXML(fileName);
		}

		private void RefreshListView()
		{
			foreach (Autodesk.Revit.DB.ViewPlan viewPlan in this.m_lstViewPlan)
			{
				ListViewItem listViewItem = new ListViewItem();
				listViewItem.Text = viewPlan.Name;
				this.lvAreas.Items.Add(listViewItem);
				List<Area> areasInViewPlan = this.GetAreasInViewPlan(viewPlan);
				List<AreaItem> list = new List<AreaItem>();
				List<Area> list2 = new List<Area>();
				foreach (Area area in areasInViewPlan)
				{
					if (!list2.Contains(area))
					{
						string text = area.GetParameter(BuiltInParameter.ROOM_NAME).AsString() + area.GetParameter("区域编号").AsString();
						AreaItem areaItem = new AreaItem();
						areaItem.dArea = area.Area;
						areaItem.sName = text;
						foreach (Area area2 in areasInViewPlan)
						{
							string b = area2.GetParameter(BuiltInParameter.ROOM_NAME).AsString() + area2.GetParameter("区域编号").AsString();
							if ((!(text == b) || !Geometry.IsEqual(area.Area, area2.Area, 0.1)) && text == b)
							{
								areaItem.dArea += area2.Area;
								list2.Add(area2);
							}
						}
						list.Add(areaItem);
					}
				}
				list.Sort(new DlgFireDefAreaResult.AreaComparer());
				for (int i = 0; i < list.Count; i++)
				{
					AreaItem areaItem2 = list.ElementAt(i);
					listViewItem = new ListViewItem();
					listViewItem.Tag = viewPlan.Name;
					listViewItem.Text = "";
					ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = areaItem2.sName;
					listViewItem.SubItems.Add(listViewSubItem);
					ListViewItem.ListViewSubItem listViewSubItem2 = new ListViewItem.ListViewSubItem();
					listViewSubItem2.Text = AssistFunc.feet2ToM2(areaItem2.dArea).ToString("#0.00");
					listViewItem.SubItems.Add(listViewSubItem2);
					this.lvAreas.Items.Add(listViewItem);
				}
			}
		}

		private List<Area> GetAreasInViewPlan(ViewPlan vp)
		{
			List<Area> list = new List<Area>();
			foreach (Element element in new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document, vp.Id).OfClass(typeof(SpatialElement)))
			{
				Area area = element as Area;
				if (area != null)
				{
					list.Add(area);
				}
			}
			return list;
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		public void PressEsc()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			DlgFireDefAreaResult.SendMessage(CmdFireDefAreaCheck._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			DlgFireDefAreaResult.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgFireDefAreaResult.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		public void ResetContinueBreak()
		{
			this.IfBreak = true;
			this.IfManualEsc = true;
		}

		private void lvAreas_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (this.lvAreas.SelectedItems.Count > 0)
			{
				ListViewItem listViewItem = this.lvAreas.SelectedItems[0];
				if (listViewItem.Tag != null)
				{
					string b = listViewItem.Tag.ToString();
					string text = listViewItem.SubItems[1].Text;
					Convert.ToDouble(listViewItem.SubItems[2].Text);
					ViewPlan viewPlan = null;
					foreach (Element element in new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document).OfClass(typeof(ViewPlan)))
					{
						viewPlan = (element as Autodesk.Revit.DB.ViewPlan);
						if (viewPlan != null && (int)viewPlan.ViewType == 116 && viewPlan.GenLevel != null && !(viewPlan.AreaScheme.Name != "防火分区面积") && !(viewPlan.Name != b))
						{
							this.m_Revit.Application.ActiveUIDocument.ActiveView = viewPlan;
							break;
						}
					}
					FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document, viewPlan.Id).OfClass(typeof(SpatialElement));
					List<Area> list = new List<Area>();
					foreach (Element element2 in filteredElementCollector)
					{
						Area area = element2 as Area;
						if (area != null)
						{
							string str = area.GetParameter(BuiltInParameter.ROOM_NAME).AsString();
							string str2 = area.GetParameter("区域编号").AsString();
							if (str + str2 == text)
							{
								list.Add(area);
							}
						}
					}
					UIDocument activeUIDocument = this.m_Revit.Application.ActiveUIDocument;
					ElementSet elementSet = new ElementSet();
					foreach (Area area2 in list)
					{
						elementSet.Insert(area2);
					}
					activeUIDocument.ShowElements(elementSet);
					List<Element> list2 = new List<Element>();
					foreach (Area item in list)
					{
						list2.Add(item);
					}
					RevitVersionFuncs.SetSelection(this.m_Revit.Application.ActiveUIDocument.Selection, list2);
					activeUIDocument.RefreshActiveView();
				}
			}
		}

		private void btnMore_Click(object sender, EventArgs e)
		{
			this.m_bExpand = !this.m_bExpand;
			if (this.m_bExpand)
			{
				this.btnMore.Text = "<<<";
			}
			else
			{
				this.btnMore.Text = ">>>";
			}
			this.ShowHideMoreParam();
		}

		private void ShowHideMoreParam()
		{
			if (this.m_bExpand)
			{
				DlgFireDefAreaResult.MoveWindow(base.Handle, base.Left, base.Top, base.Right - base.Left + 520, base.Bottom - base.Top, true);
				return;
			}
			DlgFireDefAreaResult.MoveWindow(base.Handle, base.Left, base.Top, base.Right - base.Left - 520, base.Bottom - base.Top, true);
		}

		private void gridRule_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (this.gridRule.MouseCol == 4 || this.gridRule.MouseCol == 5)
			{
				string displayText = this.gridRule.Cell(this.gridRule.MouseRow, this.gridRule.MouseCol).DisplayText;
				if (Assist.StringUtil.isNumberic2(displayText))
				{
					double listViewRowColor = Convert.ToDouble(displayText);
					this.SetListViewRowColor(listViewRowColor);
					return;
				}
			}
			else
			{
				for (int i = 0; i < this.lvAreas.Items.Count; i++)
				{
					this.lvAreas.Items[i].BackColor = (this.BackColor = System.Drawing.Color.Empty);
				}
			}
		}

		private void SetListViewRowColor(double dArea)
		{
			for (int i = 0; i < this.lvAreas.Items.Count; i++)
			{
				this.lvAreas.Items[i].BackColor = (this.BackColor = System.Drawing.Color.Empty);
			}
			for (int j = 0; j < this.lvAreas.Items.Count; j++)
			{
				ListViewItem listViewItem = this.lvAreas.Items[j];
				if (listViewItem.Tag != null && Convert.ToDouble(listViewItem.SubItems[2].Text) > dArea)
				{
					listViewItem.BackColor = System.Drawing.Color.Red;
				}
			}
		}

		private void DlgFireDefAreaResult_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
		}

		protected override bool ProcessKeyEventArgs(ref Message m)
		{
			int num = 256;
			int num2 = 27;
			if (m.Msg == num && m.WParam.ToInt32() == num2)
			{
				base.Close();
			}
			return base.ProcessKeyEventArgs(ref m);
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private ExternalCommandData m_Revit;

		private List<ViewPlan> m_lstViewPlan = new List<ViewPlan>();

		private bool m_bExpand;

		private class AreaComparer : IComparer<AreaItem>
		{
			public int Compare(AreaItem x, AreaItem y)
			{
				return x.sName.CompareTo(y.sName);
			}
		}
	}
}
